﻿/// ZaxControls
/// Programmed by Leland Ede
/// Website: http://www.zaxisstudios.com
/// E-Mail: codeplex@zaxis-studios.com
/// Create: 3/29/2014
/// Modified: 4/19/2014

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ZaxLib.Imaging;
using ZaxLib.Web.Writers.Nodes;

namespace ZaxLib.Web.UI.WebControls
{
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:NoteBox runat=server></{0}:NoteBox>")]
	public class NoteBox : ZaxControl
	{
		#region parameters

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String speed
		{
			get
			{
				String s = ViewState["Speed"] as String;
				return (s == null ? "500" : s);
			}
			set
			{
				ViewState["Speed"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String Rows
		{
			get
			{
				String s = ViewState["Rows"] as String;
				return (s == null ? "5" : s);
			}
			set
			{
				ViewState["Rows"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String Cols
		{
			get
			{
				String s = ViewState["Cols"] as String;
				return (s == null ? "40" : s);
			}
			set
			{
				ViewState["Cols"] = value;
			}
		}

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoHide
        {
            get
            {
                return Convert.ToBoolean(ViewState["AutoHide"]);
            }
            set
            {
                ViewState["AutoHide"] = value;
            }
        }

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String IconExpand
		{
			get
			{
				String s = ViewState["IconExpand"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["IconExpand"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String IconCollapse
		{
			get
			{
				String s = ViewState["IconCollapse"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["IconCollapse"] = value;
			}
		}
		#endregion

		#region Writer Methods

		private String HeadId
		{
			get
			{
				return String.Format("{0}Head", ID);
			}
		}

		private String BodyId
		{
			get
			{
				return String.Format("{0}Body", ID);
			}
		}

		private String IconId
		{
			get
			{
				return String.Format("{0}Icon", ID);
			}
		}

		protected override void PreWrite(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Id, HeadId);
			base.PreWrite(writer);
		}

		protected override void ControlWrite(HtmlTextWriter writer)
		{
			// Set the DIV id 
			writer.AddAttribute(HtmlTextWriterAttribute.Id, BodyId);
			// Start rendering div tag
			writer.RenderBeginTag(HtmlTextWriterTag.Div);
			// Clear attributes
			writer.Flush();
			// Set textarea attributes
			writer.AddAttribute(HtmlTextWriterAttribute.Rows, Rows);
			writer.AddAttribute(HtmlTextWriterAttribute.Cols, Cols);
			writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
			// Begin textarea tag
			writer.RenderBeginTag(HtmlTextWriterTag.Textarea);
			// Write the Textarea code
			writer.Write(Text);
			// End text area tag
			writer.RenderEndTag();
			// End div tag
			writer.RenderEndTag();
		}

		protected override void PostWrite(HtmlTextWriter writer)
		{
			base.PostWrite(writer);
			// Write the Javascript to the page
            if (AutoHide)
            {
				CodeWriter.CreateTopNode();
				CodeWriter.AddChild(BodyId).SelectorId();
				CodeWriter.AddChild("hide").Joined();
				CodeWriter.AddParam("");
            }// Endif AutoHide

			CodeWriter.CreateTopNode();
			CodeWriter.AddChild(HeadId).SelectorId();
			CodeWriter.AddChild("click").Joined();
			INode node = CodeWriter.AddParam("function").Function();
			CodeWriter.AddChild(BodyId, node).SelectorId();
			CodeWriter.AddChild("slideToggle").Joined();
			CodeWriter.AddParam(speed).Variable();
		}// End method PostWrite

		protected override void LabelWrite(HtmlTextWriter writer)
		{
			// Add CssClass to label
			if (CssClass != String.Empty)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
			}// Endif CssClass is defined

			switch (lblType)
			{
				case LabelType.LT_LEFT:
				case LabelType.LT_RIGHT:
					writer.RenderBeginTag(HtmlTextWriterTag.Span);
					break;
				case LabelType.LT_BOTTOM:
				case LabelType.LT_TOP:
					writer.RenderBeginTag(HtmlTextWriterTag.Div);
					break;

			}
			String iconPath = (AutoHide ? IconExpand : IconCollapse);
			// Check to see if we need to add expand image to label
			if (iconPath.Trim().Length > 0)
			{
				if (HttpContext.Current != null)
				{
					CIcon icon = new CIcon(iconPath, HttpContext.Current.Request);

					writer.AddAttribute(HtmlTextWriterAttribute.Id, IconId);
					writer.AddAttribute(HtmlTextWriterAttribute.Src, icon.WebPath);
					writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
					// Move the size to an image processing system
					writer.AddAttribute(HtmlTextWriterAttribute.Height, icon.Width);
					writer.AddAttribute(HtmlTextWriterAttribute.Width, icon.Height);
					// Render image tag
					writer.RenderBeginTag(HtmlTextWriterTag.Img);
					writer.RenderEndTag();
				}
			}// Endif IconImage is not blank

			writer.Write(Label);
			writer.RenderEndTag();
			writer.WriteLine();
			writer.Flush();
			
		}

		#endregion

		#region Constructors

		public NoteBox()
			: base()
		{
			HelpPosition = "AFTERLABEL";
		}

		#endregion

	}
}
